package com.example.controller.admin;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.common.R;
import com.example.mapper.AppointmentMapper;
import com.example.mapper.ConEmployeeServiceMapper;
import com.example.mapper.OrderMapper;
import com.example.mapper.RebackMapper;
import com.example.mapper.UserMapper;
import com.example.model.resp.ListRebackResp;

@ResponseBody
@RestController
@RequestMapping("/reback")
public class AdminRebackController {

    @Autowired
    private RebackMapper rebackMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private ConEmployeeServiceMapper conEmployeeServiceMapper;

    @GetMapping("/list")
    public R<List<ListRebackResp>> getListReback() {
        List<ListRebackResp> respList = rebackMapper.getListReback().stream()
                .map(reback -> {
                    Integer id = 0;
                    boolean isOrder = false;
                    String workerName = "";
                    String serviceName = "";
                    if (reback.getOrderId() == null) {
                        id = reback.getAppointmentId();
                        isOrder = false;
                        workerName = userMapper.getUsernameById(appointmentMapper.getApproverIdById(id));
                        serviceName = conEmployeeServiceMapper.getServiceIdByUserId();
                    } else {
                        id = reback.getOrderId();
                        isOrder = true;
                        workerName = userMapper.getUsernameById(orderMapper.getWorkerById(id));
                    }
                    String customerName = userMapper.getUsernameById(reback.getUserId());
                    return ListRebackResp.builder()
                            .id(id)
                            .isOrder(isOrder)
                            .customerName(customerName)
                            .workerName(workerName)
                            .serviceName(serviceName)
                            .build();
                })
                .collect(Collectors.toList());
        return R.success(respList);
    }
}
